package com.jiangkai.common.generator;


import org.junit.Test;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; 
/**
 * 
 *  自动生成代码 
 * @author Administrator
 *
 */
public class SimpleMp {
	
	 @Test
	  public void generateCode() { 
	    //指定包名 
	    String packageName = "com.jiangkai.generator"; 
	    //user -> UserService, 设置成true: user -> IUserService 
	    boolean serviceNameStartWithI = false; 
	    //指定生成的表名 
	    String[] tableNames = new String[]{"sys_user_role"}; 
	    generateByTables(serviceNameStartWithI, packageName, tableNames); 
	  } 
	 
	 /** 
	   * 根据表自动生成 
	   * 
	   * @param serviceNameStartWithI 默认为false 
	   * @param packageName      包名 
	   * @param tableNames      表名 
	   * @author Terry 
	   */
	  private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) { 
	    //配置数据源 
	    DataSourceConfig dataSourceConfig = getDataSourceConfig(); 
	    // 策略配置 
	    StrategyConfig strategyConfig = getStrategyConfig(tableNames); 
	    //全局变量配置 
	    GlobalConfig globalConfig = getGlobalConfig(serviceNameStartWithI); 
	    //包名配置 
	    PackageConfig packageConfig = getPackageConfig(packageName); 
	    //自动生成 
	    atuoGenerator(dataSourceConfig, strategyConfig, globalConfig, packageConfig); 
	  } 
	  /** 
	   * 集成 
	   * 
	   * @param dataSourceConfig 配置数据源 
	   * @param strategyConfig  策略配置 
	   * @param config      全局变量配置 
	   * @param packageConfig  包名配置 
	   * @author Terry 
	   */
	  private void atuoGenerator(DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig, GlobalConfig config, PackageConfig packageConfig) { 
		  AutoGenerator aug =  new AutoGenerator();
		  aug.setGlobalConfig(config);
		  aug.setDataSource(dataSourceConfig);
		  aug.setStrategy(strategyConfig);
		  aug.setPackageInfo(packageConfig); 
		  aug.execute(); 
	  } 
	  /** 
	   * 设置包名 
	   * 
	   * @param packageName 父路径包名 
	   * @return PackageConfig 包名配置 
	   * @author Terry 
	   */
	  private PackageConfig getPackageConfig(String packageName) { 
		  PackageConfig pc =  new PackageConfig();
		  pc.setParent(packageName);
		  pc.setXml("mapper");
		  pc.setMapper("dao");
		  pc.setController("controller");
		  pc.setEntity("entity"); 
		  return pc;
	  } 
	  /** 
	   * 全局配置 
	   * 
	   * @param serviceNameStartWithI false 
	   * @return GlobalConfig 
	   * @author Terry 
	   */
	  private GlobalConfig getGlobalConfig(boolean serviceNameStartWithI) { 
	    GlobalConfig globalConfig = new GlobalConfig(); 
	    globalConfig.setBaseColumnList(true);
	    globalConfig.setBaseResultMap(true);
	    globalConfig.setActiveRecord(false);
	    globalConfig.setAuthor("Terry");
	        //设置输出路径 
	    globalConfig.setOutputDir(getOutputDir("MavenSsm"));
	    globalConfig.setFileOverride(true); 
	    if (!serviceNameStartWithI) { 
	      //设置service名 
	      globalConfig.setServiceName("%sService"); 
	    } 
	    return globalConfig; 
	  } 
	  /** 
	   * 返回项目路径 
	   * 
	   * @param projectName 项目名 
	   * @return 项目路径 
	   * @author Terry 
	   */
	  private String getOutputDir(String projectName) { 
	    String path = this.getClass().getClassLoader().getResource("").getPath(); 
	    int index = path.indexOf(projectName); 
	    return path.substring(1, index) + projectName + "/src/main/java/"; 
	  } 
	  /** 
	   * 策略配置 
	   * 
	   * @param tableNames 表名 
	   * @return StrategyConfig 
	   * @author Terry 
	   */
	  private StrategyConfig getStrategyConfig(String... tableNames) { 
		  StrategyConfig sc = new StrategyConfig();
		  sc.setCapitalMode(true);
		  sc.setEntityLombokModel(false);
		  sc.setDbColumnUnderline(true);
		  sc.setNaming(NamingStrategy.underline_to_camel);
		  sc.setInclude(tableNames); 
	     return sc;
	  } 
	  /** 
	   * 配置数据源 
	   * 
	   * @return 数据源配置 DataSourceConfig 
	   * @author Terry 
	   */
	  private DataSourceConfig getDataSourceConfig() { 
	    String dbUrl = "jdbc:mysql://localhost:3306/jeeplus_ani_big"; 
	    return new DataSourceConfig().setDbType(DbType.MYSQL) 
	        .setUrl(dbUrl) 
	        .setUsername("root") 
	        .setPassword("") 
	        .setDriverName("com.mysql.jdbc.Driver"); 
	  } 
	  /** 
	   * 根据表自动生成 
	   * 
	   * @param packageName 包名 
	   * @param tableNames 表名 
	   * @author Terry 
	   */
	  @SuppressWarnings("unused") 
	  private void generateByTables(String packageName, String... tableNames) { 
	    generateByTables(true, packageName, tableNames); 
	  } 
	}
